﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;

using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Symbols;

namespace eGrid_App
{
    /// <summary>
    /// Interaction logic for FeatureInfoTip.xaml
    /// </summary>
    public partial class FeatureInfoTip : ScatterViewItem
    {
        ScatterLayer parentLayer = null;
        TroubleTicket myFeature;
        TroubleTicketServerLayer serverlayer;

        #region constructors
        public FeatureInfoTip(ScatterLayer parent, TroubleTicket g, TroubleTicketServerLayer ttsl)
        {
            InitializeComponent();
            myFeature = g;
            parentLayer = parent;
            serverlayer = ttsl;

            // The server provides the severity as a short, while the control saves it as an int. When making the window, we must handle both cases.
            if (myFeature.originalGraphic.Attributes["Severity"] is System.Int16)
            {
                Priority = (short)(myFeature.originalGraphic.Attributes["Severity"]);
            }
            else if (myFeature.originalGraphic.Attributes["Severity"] is System.Int32)
            {
                Priority = (int)(myFeature.originalGraphic.Attributes["Severity"]);
            }

            TT_Num.Text = myFeature.originalGraphic.Attributes["OBJECTID"].ToString();
            TT_DES.Text = myFeature.originalGraphic.Attributes["Description"].ToString(); 
            TT_LOC.Text = "X: " + ((MapPoint)myFeature.Geometry).X + " Y: " + ((MapPoint)myFeature.Geometry).Y; 

            ShowMapButton.Visibility = Visibility.Visible;

        }


        #endregion

        #region event handlers

        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            if (EditButton.Content.Equals("Edit"))
            {
                SwitchToEditMode();
                return;
            }

            if (EditButton.Content.Equals("Done Edit"))
            {
                SwitchToViewMode();
                return;
            }
        }

        private void ShowMapButton_Click(object sender, RoutedEventArgs e)
        {
            OpenMapFrame();
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            parentLayer.RemoveFeatureTip(this);
        }

        private void SS_SEV_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            Priority = (int)e.NewValue;
            if (myFeature != null)
            {

                serverlayer.TicketSeverityChanged((int)myFeature.originalGraphic.Attributes["OBJECTID"], (int)e.NewValue);
                myFeature.originalGraphic.Attributes["Severity"] = (int)e.NewValue;
            }
        }
        #endregion


        #region utility functions
        
        private void SwitchToEditMode()
        {

            TT_DES.IsEnabled = true;
            SS_SEV.Visibility = Visibility.Visible;
            EditButton.Background = new SolidColorBrush(Colors.Red);
            EditButton.Content = "Done Edit";
            /* Cannot exit when editing */
            CloseButton.IsEnabled = false;
            CloseButton.Background = new SolidColorBrush(Colors.Gray);
            UpdateLayout();

        }
        
        private void SwitchToViewMode()
        {
 
            TT_DES.IsEnabled = false;
            SS_SEV.Visibility = Visibility.Collapsed;
            EditButton.Background = new SolidColorBrush(Colors.Aqua);
            EditButton.Content = "Edit";
            /* Re-enable the close button to allow the user to close the MapTip */
            CloseButton.IsEnabled = true;
            CloseButton.Background = new SolidColorBrush(Colors.White);
            /* Update the trouble ticket */
            myFeature.originalGraphic.Attributes["Description"] = TT_DES.Text; 
            UpdateLayout();

        }

        private void OpenMapFrame()
        {
            this.Visibility = Visibility.Hidden;

            if (parentLayer == null) return;

            // FIX ME: the orientation and position of the map frame
            //parentLayer.CreateMapFrameFromBookmarkPin(ttp.Bookmark, Center, Orientation);

            MapPoint bottomLeft = new MapPoint();
            MapPoint topRight = new MapPoint();
            bottomLeft.X = ((MapPoint)myFeature.Geometry).X - 0.01;
            bottomLeft.Y = ((MapPoint)myFeature.Geometry).Y - 0.01;
            topRight.X = ((MapPoint)myFeature.Geometry).X + 0.01;
            topRight.Y = ((MapPoint)myFeature.Geometry).Y + 0.01;
            
            Point bottomLeftp = new Point();
            Point topRightp = new Point();
            Point featureScreenPosition = parentLayer.BackgroundMap.MyMap.MapToScreen((MapPoint)myFeature.Geometry);
            bottomLeftp.X = featureScreenPosition.X - 135;
            bottomLeftp.Y = featureScreenPosition.Y - 135;
            topRightp.X = featureScreenPosition.X + 135;
            topRightp.Y = featureScreenPosition.Y + 135;


            parentLayer.CreateMapFrameFromMapPoints(bottomLeft, topRight, bottomLeftp, topRightp);
        }

        // the priority property controls only the priority graphics for the window, and not the value stored in the feature backing it
        public int Priority
        {
            get
            {
                return (int)myFeature.originalGraphic.Attributes["Severity"];
            }
            set
            {

                if (myFeature != null)
                {
                    SS_SEV.Value = value;
                    switch (value)
                    {
                        case 1:
                            TT_SEV.Foreground = new SolidColorBrush(Colors.Green);
                            TT_SEV.Text = "Low";
                            break;
                        case 2:
                            TT_SEV.Foreground = new SolidColorBrush(Colors.Yellow);
                            TT_SEV.Text = "Medium";
                            break;
                        case 3:
                            TT_SEV.Foreground = new SolidColorBrush(Colors.Red);
                            TT_SEV.Text = "High";
                            break;
                    }
                }    
            }
        }



        #endregion
    }
}
